﻿using System;
using Manager;
using UnityEngine;

// Token: 0x020002BE RID: 702
public class CustomTextureControl
{
	// Token: 0x06001116 RID: 4374 RVA: 0x00088A4C File Offset: 0x00086C4C
	public CustomTextureControl()
	{
		this.InitEnd = false;
		this.matDraw = null;
	}

	// Token: 0x17000267 RID: 615
	// (get) Token: 0x06001117 RID: 4375 RVA: 0x00088A64 File Offset: 0x00086C64
	// (set) Token: 0x06001118 RID: 4376 RVA: 0x00088A6C File Offset: 0x00086C6C
	public Material matDraw { get; private set; }

	// Token: 0x17000268 RID: 616
	// (get) Token: 0x06001119 RID: 4377 RVA: 0x00088A78 File Offset: 0x00086C78
	// (set) Token: 0x0600111A RID: 4378 RVA: 0x00088A80 File Offset: 0x00086C80
	public bool InitEnd { get; private set; }

	// Token: 0x0600111B RID: 4379 RVA: 0x00088A8C File Offset: 0x00086C8C
	public virtual void Initialize(string drawMatABName, string drawMatName, string createMatABName, string createMatName, int width, int height)
	{
		this.InitEnd = false;
		this.matDraw = global::CommonLib.LoadAsset<Material>(drawMatABName, drawMatName, true, string.Empty);
		if (null == this.matDraw)
		{
			return;
		}
		global::Singleton<Manager.Character>.Instance.AddLoadAssetBundle(drawMatABName);
		this.matCreate = global::CommonLib.LoadAsset<Material>(createMatABName, createMatName, true, string.Empty);
		if (null == this.matCreate)
		{
			UnityEngine.Object.Destroy(this.matDraw);
			this.matDraw = null;
			return;
		}
		global::Singleton<Manager.Character>.Instance.AddLoadAssetBundle(createMatABName);
		this.texMain = this.matCreate.GetTexture(global::Singleton<Manager.Character>.Instance._MainTex);
		/*if (Manager.Config.BasicSettings.enable4KSkinDiffuse)
		{
			width = 4096;
			height = 4096;
		}*/
		this.createTex = new RenderTexture(width, height, 0);
		/*if (Manager.Config.BasicSettings.enableSkinDiffuseMipMap)
		{
			this.createTex.filterMode = FilterMode.Trilinear;
			this.createTex.useMipMap = true;
		}*/
		this.InitEnd = true;
	}

	// Token: 0x0600111C RID: 4380 RVA: 0x00088B80 File Offset: 0x00086D80
	public virtual void Release()
	{
		UnityEngine.Object.Destroy(this.createTex);
		this.createTex = null;
		UnityEngine.Object.Destroy(this.matDraw);
		this.matDraw = null;
		UnityEngine.Object.Destroy(this.matCreate);
		this.matCreate = null;
		this.InitEnd = false;
	}

	// Token: 0x0600111D RID: 4381 RVA: 0x00088BC0 File Offset: 0x00086DC0
	public virtual void SetMainTexture(Texture tex)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.texMain = tex;
	}

	// Token: 0x0600111E RID: 4382 RVA: 0x00088BD4 File Offset: 0x00086DD4
	public virtual void SetTexture(string propertyName, Texture tex)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.matCreate.SetTexture(propertyName, tex);
	}

	// Token: 0x0600111F RID: 4383 RVA: 0x00088BEC File Offset: 0x00086DEC
	public virtual void SetTexture(int propertyID, Texture tex)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.matCreate.SetTexture(propertyID, tex);
	}

	// Token: 0x06001120 RID: 4384 RVA: 0x00088C04 File Offset: 0x00086E04
	public virtual void SetColor(string propertyName, Color color)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.matCreate.SetColor(propertyName, color);
	}

	// Token: 0x06001121 RID: 4385 RVA: 0x00088C1C File Offset: 0x00086E1C
	public virtual void SetColor(int propertyID, Color color)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.matCreate.SetColor(propertyID, color);
	}

	// Token: 0x06001122 RID: 4386 RVA: 0x00088C34 File Offset: 0x00086E34
	public virtual void SetOffsetAndTiling(string propertyName, float tx, float ty, float ox, float oy)
	{
		if (!this.InitEnd)
		{
			return;
		}
		this.matCreate.SetTextureOffset(propertyName, new Vector2(ox, oy));
		this.matCreate.SetTextureScale(propertyName, new Vector2(tx, ty));
	}

	// Token: 0x06001123 RID: 4387 RVA: 0x00088C68 File Offset: 0x00086E68
	public virtual void SetOffsetAndTiling(string propertyName, int baseW, int baseH, int addW, int addH, float addPx, float addPy)
	{
		if (!this.InitEnd)
		{
			return;
		}
		if (addPx >= 0f & addPy >= 0f & (float)addW + addPx <= 1024f & (float)addH + addPy <= 1024f)
		{
			baseW = 1024;
			baseH = 1024;
		}
		else
		{
			addPx = Mathf.Abs(addPx);
			addPy = Mathf.Abs(addPy);
			baseH = 4096;
			baseW = 4096;
		}
		float num = (float)baseW / (float)addW;
		float num2 = (float)baseH / (float)addH;
		float ox = -(addPx / (float)baseW) * num;
		float oy = -(((float)baseH - addPy - (float)addH) / (float)baseH) * num2;
		this.SetOffsetAndTiling(propertyName, num, num2, ox, oy);
	}

	// Token: 0x06001124 RID: 4388 RVA: 0x00088D20 File Offset: 0x00086F20
	public virtual bool RebuildTextureAndSetMaterial()
	{
		if (!this.InitEnd)
		{
			return false;
		}
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = null;
		bool sRGBWrite = GL.sRGBWrite;
		GL.sRGBWrite = false;
		Graphics.SetRenderTarget(this.createTex);
		GL.Clear(false, true, Color.white);
		Graphics.SetRenderTarget(null);
		Graphics.Blit(this.texMain, this.createTex, this.matCreate, 0);
		GL.sRGBWrite = sRGBWrite;
		this.matDraw.SetTexture(global::Singleton<Manager.Character>.Instance._MainTex, this.createTex);
		RenderTexture.active = active;
		return true;
	}

	// Token: 0x040016D4 RID: 5844
	protected RenderTexture createTex;

	// Token: 0x040016D5 RID: 5845
	protected Material matCreate;

	// Token: 0x040016D6 RID: 5846
	protected Texture texMain;
}
